package com.wyl.main

import androidx.fragment.app.Fragment
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.extend.transact
import com.wyl.main.databinding.MainActivityBinding

class MainActivity : BindingActivity<MainActivityBinding>() {

    private val serviceFragment by lazy { ServiceFragment() }
    private val informationFragment by lazy { InformationFragment() }
    private val interactionFragment by lazy { InteractionFragment() }
    private val orderFragment by lazy { OrderFragment() }
    private val mineFragment by lazy { MineFragment() }


//    private val navFragment by lazy {
//        getFragment(CategoryFragment) { withString("type", "Navigation") }
//
//    }


    override fun getLayoutId(): Int = R.layout.main_activity

    override fun initView() {

        transact { replace(R.id.rootView, serviceFragment) }

        binding.tab.material()
            .addItem(R.drawable.service_normal, R.drawable.service_selected, "服务")
            .addItem(R.drawable.information_normal, R.drawable.information_selected, "资讯")
            .addItem(R.drawable.interaction_normal, R.drawable.interaction_selected, "互动")
            .addItem(R.drawable.order_normal, R.drawable.order_selected, "订单")
            .addItem(R.drawable.mine_normal, R.drawable.mine_selected, "我的")
            .dontTintIcon()
            .build()
            .addSimpleTabItemSelectedListener { index, old ->
                when (index) {
                    0 -> show(serviceFragment)
                    1 -> show(informationFragment)
                    2 -> show(interactionFragment)
                    3 -> show(orderFragment)
                    4 -> show(mineFragment)
                }
            }
    }

    private fun show(fragment: Fragment) {
        if (fragment.isAdded) {
            hide(serviceFragment)
            hide(informationFragment)
            hide(interactionFragment)
            hide(orderFragment)
            hide(mineFragment)
            transact { show(fragment) }
        } else {
            transact { add(R.id.rootView, fragment) }
        }
    }

    private fun hide(fragment: Fragment) {
        if (fragment.isAdded) {
            transact { hide(fragment) }
        }
    }

    override fun loadData() {

    }

    override fun onBackPressed() {
        super.onBackPressed()
    }

}
